import React from "react";
import {asyncComponent} from "react-async-component";
import {Redirect, Route, Switch} from "react-router-dom"
import {Tools} from "weaver-mobile";
import {FloatingTouch, DocumentTitle} from "weaver-mobile-page";
import AccessResult from "./accessResult"
import AccessAudit from "./accessAudit"
import AccessView from "./accessView"
import ProgramView from "./programView"
import ProgramAudit from "./programAudit"
import Program from "./program"
import Report from "./report"
import ProgramItem from "./programItem"
import {observable} from "mobx";
import {WrAlertPage} from "../../public";

const getLabel = Tools.getLabel;

/**
 * 绩效
 */
export default class PerformanceMain extends React.Component {

    constructor(props) {
        super(props);
        window.RefreshAccessNum = () => {
            this.updateNum();
        }
        this.state = {
            menuData: [
                {path: "/performance/accessResult", menuname: getLabel(506161, "考核成绩"),},
                {path: "/performance/accessAudit", menuname: getLabel(506162, "考核评分")},
                {path: "/performance/programView", menuname: getLabel(506163, "我的考核方案")},
                {path: "/performance/programAudit", menuname: getLabel(506164, "待审核方案")},
                {path: "/performance/program", menuname: getLabel(506165, "方案设置查询")},
                {path: "/performance/report", menuname: getLabel(506167, "考核报表")},
            ],
            isRight: "", //是否开启了绩效管理模块
            hasRight: "",//是否拥有GP_BaseSettingMaint权限
        }
    }

    componentWillMount() {
        let options = {
            method: 'GET',
            url: '/api/workrelate/common/checkHasRigth',
            params: {id: 3}
        };
        Tools.callApi(options).then((data) => {
            if (data.status && data.isRight) {
                this.setState({isRight: '1'})
                this.updateNum();
            } else {
                this.setState({isRight: "0"});
            }
        })
    }

    render() {
        const {isRight} = this.state;
        if (isRight == "") {
            return <div></div>;
        }
        if (isRight == "0") {
            return <WrAlertPage show={true} msg={getLabel(509311, "您的系统未开启绩效管理模块")}/>
        }
        const data = [
            {match: /^\/performance\/accessResult/i, title: () => getLabel(506161, "考核成绩")},
            {match: /^\/performance\/accessResult\/accessView/i, title: () => getLabel(513694, "评分详情")},
            {match: /^\/performance\/accessAudit/i, title: () => getLabel(506162, "考核评分")},
            {match: /^\/performance\/accessAudit\/accessView/i, title: () => getLabel(513694, "评分详情")},
            {match: /^\/performance\/accessView/i, title: () => getLabel(513694, "评分详情")},
            {match: /^\/performance\/program/i, title: () => getLabel(506165, "方案设置查询")},
            {match: /^\/performance\/program\/programView/i, title: () => getLabel(18056, "考核方案")},
            {match: /^\/performance\/programAudit/i, title: () => getLabel(506164, "待审核方案")},
            {match: /^\/performance\/report/i, title: () => getLabel(506167, "考核报表")},
            {match: /^\/performance\/report\/accessView/i, title: () => getLabel(513694, "评分详情")},
            {match: /^\/performance\/programView/i, title: () => getLabel(18056, "考核方案")},
        ];
        return (
            <div>
                <DocumentTitle data={data} defaultTitle=""/>
                <Switch>
                    <Route id="accessResult"
                           breadcrumbName="考核成绩"
                           path="/performance/accessResult"
                           render={() => <AccessResult menu={this.state.menuData}/>}/>
                    <Route id="accessAudit"
                           breadcrumbName="考核评分"
                           path="/performance/accessAudit"
                           render={() => <AccessAudit menu={this.state.menuData}/>}/>
                    <Route id="accessView"
                           breadcrumbName="评分详情"
                           path="/performance/accessView"
                           render={() => <AccessView menu={this.state.menuData}/>}/>
                    <Route id="program"
                           breadcrumbName="方案设置查询"
                           path="/performance/program"
                           render={() => <Program menu={this.state.menuData}/>}/>
                    <Route id="programAudit"
                           breadcrumbName="待审核方案"
                           path="/performance/programAudit"
                           render={() => <ProgramAudit menu={this.state.menuData}/>}/>
                    <Route id="programView"
                           breadcrumbName="我的考核方案"
                           path="/performance/programView"
                           render={() => <ProgramView menu={this.state.menuData}/>}/>
                    <Route id="report"
                           breadcrumbName="考核报表"
                           path="/performance/report"
                           render={() => <Report menu={this.state.menuData}/>}/>
                    <Redirect exact path="/performance" to={{pathname: "/performance/accessResult"}}/>
                </Switch>
            </div>
        )
    }

    updateNum = () => {
        let options = {
            method: 'GET',
            url: '/api/workrelate/performance/accessAuditProgramNum',
            params: {operation: "all"}
        };
        Tools.callApi(options).then((data) => {
            if (data.api_status) {
                this.setState({
                    menuData: [
                        {path: "/performance/accessResult", menuname: getLabel(506161, "考核成绩"),},
                        {
                            path: "/performance/accessAudit",
                            menuname: getLabel(506162, "考核评分"),
                            rightContent: data.scount
                        },
                        {path: "/performance/programView", menuname: getLabel(506163, "我的考核方案")},
                        {
                            path: "/performance/programAudit",
                            menuname: getLabel(506164, "待审核方案"),
                            rightContent: data.auditamount
                        },
                        {
                            path: "/performance/program",
                            menuname: getLabel(506165, "方案设置查询"),
                            rightContent: data.nosetamount,
                            showRed: false,
                        },
                        {path: "/performance/report", menuname: getLabel(506167, "考核报表")},
                    ]
                });
            }
        });
    }
}